package org.example.Servlet.TopicServlet;

import org.example.Dao.VoteDao;
import org.example.entity.Person;
import org.example.entity.Topic;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/history/show")
public class ShowTopicHistory extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Person person = (Person) session.getAttribute("person");
        int pid = person.getPid();
        VoteDao voteDao = new VoteDao();
        try {
            List<Topic> topicList = voteDao.getMyVoted(pid);
            session.setAttribute("history", topicList);
            session.setAttribute("historyMsg", "");
            resp.sendRedirect(req.getContextPath() + "/views/topic/history.jsp");
        } catch (Exception e) {
            System.out.println("topic history error ==> " + e.getMessage());
            session.setAttribute("historyMsg", "诶呀，溜号了");
            resp.sendRedirect(req.getContextPath() + "/person/index");
        }
    }
}
